package s.f.util;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 *
 * @author Froy A.
 */
public class Conexion {

    public static final String SERVER = "192.168.1.2";
    public static final String USER_SERVER = "postgres";
    public static final String PASS_SERVER = "postgres";
    public static final String DB_SERVER = "personalsf";
    public static final String DRIVER = "org.postgresql.Driver";
    public static final String URL_SERVER = "jdbc:postgresql://" + SERVER
            + ":5432/" + DB_SERVER;

    /**
     * Establece la conexión a la base de datos.
     * @return Regresa una conexión.
     * @throws AppException 
     */
    public static Connection getConnection() throws AppException {
        try {
            Class.forName(DRIVER);
            return DriverManager.getConnection(
                    URL_SERVER, USER_SERVER, PASS_SERVER);
        } catch (Exception ex) {
            throw new AppException("Fallo al intentar realizar la conexión", ex);
        } catch (Throwable th) {
            throw new AppException("Error Inesperado", th);
        }
    }

    /**
     * Cierra la conexión a la base de datos.
     * @param connection Solicita la conexión para cerrarla.
     * @throws AppException 
     */
    public static void closeConnection(Connection connection) throws AppException {
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception ex) {
                throw new AppException("Error al cerrar la conexión", ex);
            }
        }
    }
}
